package pck_gui;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pck_utilitarios.Cls_Conexion;
import pck_utilitarios.Utiles_Panel;
import udprrhh.UDPRRHHView;
/**
 *
 * @author supermiguelon
 */
public class JP_Asignar_Jefatura_Unidad_Organica extends pck_utilitarios.JP_Modelo {
    private String dni_jefe;
    /** Creates new form JP_Asignar_Jefatura_Unidad_Organica */
    public JP_Asignar_Jefatura_Unidad_Organica() {
        initComponents();
        llenarTabla();
    }
    public void limpiar(){
        TXT_Codigo.setText("");
        TXT_Nombre_Unidad.setText("");
        TXT_Jefatura.setText("");
        BTN_Buscar_Jefatura.setEnabled(false);
        BTN_Actualiza.setEnabled(false);
        BTN_Eliminar.setEnabled(false);
        TXT_Nivel.setText("");
        TXT_CodDepen.setText("");
    }
    public void obtenerJefe(String dni){
        dni_jefe = dni;
        String cadena = "SELECT     codigo_persona, Ap_paterno+' '+Ap_materno+' '+Nombre as jefe ";
        cadena = cadena + "FROM         Persona where codigo_persona = '"+dni+"'";
        try{
            Connection cn = Cls_Conexion.obtenerConexion();
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(cadena);
            while(rs.next()){                
                TXT_Jefatura.setText(rs.getString("jefe"));
            }
            rs.close();
            st.close();
            cn.close();
        }catch(Exception s){

        }
    }
    public void obtenerUnidad(String codigo){
        String cadena = "";
        cadena = "SELECT     Persona.Ap_paterno + '' + Persona.Ap_materno + ' ' + Persona.Nombre AS jefe,Persona.codigo_persona,  ";
        cadena = cadena + "Unidad_Organizativa.Unidad_Organizativa, Unidad_Organizativa.cod_Uni_Organizativa, ";
        cadena = cadena + "responsable_uni_organi.nivel,responsable_uni_organi.codigo_area_depende ";
        cadena = cadena + "FROM responsable_uni_organi INNER JOIN ";
        cadena = cadena + "Persona ON responsable_uni_organi.dni_trabajador_responsa = Persona.codigo_persona RIGHT OUTER JOIN ";
        cadena = cadena + "Unidad_Organizativa ON responsable_uni_organi.cod_uni_organizativa = Unidad_Organizativa.cod_Uni_Organizativa ";
        cadena = cadena + "where Unidad_Organizativa.cod_Uni_Organizativa = '"+codigo+"' ";
        Connection cn = Cls_Conexion.obtenerConexion();
        try{
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(cadena);           
            while(rs.next()){
                TXT_Codigo.setText(rs.getString("cod_uni_organizativa"));
                TXT_Nombre_Unidad.setText(rs.getString("Unidad_Organizativa"));
                TXT_Jefatura.setText(rs.getString("jefe"));
                dni_jefe = rs.getString("codigo_persona");
                TXT_Nivel.setText(rs.getString("nivel"));
                TXT_CodDepen.setText(rs.getString("codigo_area_depende"));
            }
            rs.close();
            st.close();
            cn.close();
        }catch(Exception s){

        }
        BTN_Actualiza.setEnabled(true);
        BTN_Eliminar.setEnabled(true);
        BTN_Buscar_Jefatura.setEnabled(true);
    }
    public void llenarTabla(){
        String cadena = "";
        cadena = "SELECT responsable_uni_organi.cod_uni_organizativa, Unidad_Organizativa.Unidad_Organizativa, ";
        cadena = cadena + "Persona.Ap_paterno+' '+Persona.Ap_materno+' '+Persona.Nombre as jefe ";
        cadena = cadena + "FROM responsable_uni_organi INNER JOIN ";
        cadena = cadena + "Unidad_Organizativa ON responsable_uni_organi.cod_uni_organizativa = ";
        cadena = cadena + "Unidad_Organizativa.cod_Uni_Organizativa INNER JOIN ";
        cadena = cadena + "Persona ON responsable_uni_organi.dni_trabajador_responsa = Persona.codigo_persona order by Unidad_Organizativa ";
              
        Connection cn = Cls_Conexion.obtenerConexion();
        try{
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(cadena);
            String fila[];
            DefaultTableModel mode = (DefaultTableModel)this.TBL_Unidades.getModel();
            while(mode.getRowCount()>0){
                mode.removeRow(0);
            }
            while(rs.next()){
                fila = new String[3];
                fila[0] = rs.getString("cod_uni_organizativa");
                fila[1] = rs.getString("Unidad_Organizativa");
                fila[2] = rs.getString("jefe");
                mode.addRow(fila);
            }
            rs.close();
            st.close();
            cn.close();
        }catch(Exception s){

        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TBL_Unidades = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        TXT_Codigo = new javax.swing.JTextField();
        TXT_Nombre_Unidad = new javax.swing.JTextField();
        TXT_Jefatura = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        BTN_Buscar_Jefatura = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        BTN_Actualiza = new javax.swing.JButton();
        BTN_Eliminar = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        TXT_Nivel = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        TXT_CodDepen = new javax.swing.JTextField();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(udprrhh.UDPRRHHApp.class).getContext().getResourceMap(JP_Asignar_Jefatura_Unidad_Organica.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        TBL_Unidades.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cod. Unid. Organica", "Unidad Organica", "Jefatura"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TBL_Unidades.setName("TBL_Unidades"); // NOI18N
        TBL_Unidades.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TBL_UnidadesKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(TBL_Unidades);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(10, 320, 600, 110);

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(160, 290, 290, 15);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(50, 30, 80, 14);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel1.add(jLabel3);
        jLabel3.setBounds(50, 60, 50, 14);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel1.add(jLabel4);
        jLabel4.setBounds(50, 100, 60, 14);

        TXT_Codigo.setEditable(false);
        TXT_Codigo.setText(resourceMap.getString("TXT_Codigo.text")); // NOI18N
        TXT_Codigo.setName("TXT_Codigo"); // NOI18N
        jPanel1.add(TXT_Codigo);
        TXT_Codigo.setBounds(120, 30, 150, 20);

        TXT_Nombre_Unidad.setEditable(false);
        TXT_Nombre_Unidad.setText(resourceMap.getString("TXT_Nombre_Unidad.text")); // NOI18N
        TXT_Nombre_Unidad.setName("TXT_Nombre_Unidad"); // NOI18N
        jPanel1.add(TXT_Nombre_Unidad);
        TXT_Nombre_Unidad.setBounds(120, 60, 360, 20);

        TXT_Jefatura.setEditable(false);
        TXT_Jefatura.setText(resourceMap.getString("TXT_Jefatura.text")); // NOI18N
        TXT_Jefatura.setName("TXT_Jefatura"); // NOI18N
        jPanel1.add(TXT_Jefatura);
        TXT_Jefatura.setBounds(120, 90, 270, 20);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(310, 30, 170, 23);

        BTN_Buscar_Jefatura.setText(resourceMap.getString("BTN_Buscar_Jefatura.text")); // NOI18N
        BTN_Buscar_Jefatura.setEnabled(false);
        BTN_Buscar_Jefatura.setName("BTN_Buscar_Jefatura"); // NOI18N
        BTN_Buscar_Jefatura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_Buscar_JefaturaActionPerformed(evt);
            }
        });
        jPanel1.add(BTN_Buscar_Jefatura);
        BTN_Buscar_Jefatura.setBounds(400, 90, 73, 23);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(null);

        BTN_Actualiza.setText(resourceMap.getString("BTN_Actualiza.text")); // NOI18N
        BTN_Actualiza.setEnabled(false);
        BTN_Actualiza.setName("BTN_Actualiza"); // NOI18N
        BTN_Actualiza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_ActualizaActionPerformed(evt);
            }
        });
        jPanel2.add(BTN_Actualiza);
        BTN_Actualiza.setBounds(20, 20, 100, 23);

        BTN_Eliminar.setText(resourceMap.getString("BTN_Eliminar.text")); // NOI18N
        BTN_Eliminar.setEnabled(false);
        BTN_Eliminar.setName("BTN_Eliminar"); // NOI18N
        BTN_Eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_EliminarActionPerformed(evt);
            }
        });
        jPanel2.add(BTN_Eliminar);
        BTN_Eliminar.setBounds(140, 20, 90, 23);

        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jPanel2.add(jButton6);
        jButton6.setBounds(260, 20, 110, 23);

        jPanel1.add(jPanel2);
        jPanel2.setBounds(120, 220, 410, 60);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel1.add(jLabel5);
        jLabel5.setBounds(50, 130, 60, 14);

        TXT_Nivel.setText(resourceMap.getString("TXT_Nivel.text")); // NOI18N
        TXT_Nivel.setName("TXT_Nivel"); // NOI18N
        jPanel1.add(TXT_Nivel);
        TXT_Nivel.setBounds(120, 120, 280, 20);

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel1.add(jLabel6);
        jLabel6.setBounds(50, 160, 140, 14);

        TXT_CodDepen.setText(resourceMap.getString("TXT_CodDepen.text")); // NOI18N
        TXT_CodDepen.setName("TXT_CodDepen"); // NOI18N
        jPanel1.add(TXT_CodDepen);
        TXT_CodDepen.setBounds(200, 160, 270, 20);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JInternalFrame jif_Unidad_Organica_Buscar;
        JP_Unidad_Organica_Buscar panel =  new JP_Unidad_Organica_Buscar(1);
        jif_Unidad_Organica_Buscar = Utiles_Panel.creaVentanaInternaModal((UDPRRHHView)this.getRef(), panel, "Busca de Unidades Organicas");
        panel.setPadre(Utiles_Panel.TIPO_JINTERNALFRAME, jif_Unidad_Organica_Buscar);
        panel.setRefDep(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void BTN_Buscar_JefaturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_Buscar_JefaturaActionPerformed
        JInternalFrame jif_trabajador_Buscar;
        JP_Trabajador_Buscar panel =  new JP_Trabajador_Buscar();
        jif_trabajador_Buscar = Utiles_Panel.creaVentanaInternaModal((UDPRRHHView)this.getRef(), panel, "Busca de Especialidades");
        panel.setPadre(Utiles_Panel.TIPO_JINTERNALFRAME, jif_trabajador_Buscar);
        panel.setRefDep(this);
    }//GEN-LAST:event_BTN_Buscar_JefaturaActionPerformed

    private void BTN_ActualizaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_ActualizaActionPerformed
        String cadena = "select COUNT(*) as contador from responsable_uni_organi where cod_uni_organizativa = '"+TXT_Codigo.getText()+"'";
        int contador = 0;
        Connection cn;
        Statement st;
        try{
            cn = Cls_Conexion.obtenerConexion();
            st = cn.createStatement();
            ResultSet rs = st.executeQuery(cadena);
            while (rs.next()){
                contador = rs.getInt("contador");
            }
            if ( contador == 0 ){
                cadena = "insert into responsable_uni_organi(cod_uni_organizativa,dni_trabajador_responsa,dni_trabajador_encargado,nivel,codigo_area_depende) ";
                cadena = cadena + "values ('"+TXT_Codigo.getText()+"','"+dni_jefe+"','',"+this.TXT_Nivel.getText().trim()+",'"+this.TXT_CodDepen.getText().trim()+"')";
            }else{
                cadena = "update responsable_uni_organi set dni_trabajador_responsa = '"+dni_jefe+"', ";
                cadena = cadena + "nivel="+this.TXT_Nivel.getText().trim()+",codigo_area_depende='"+this.TXT_CodDepen.getText().trim()+"' ";
                cadena = cadena + " where cod_uni_organizativa = '"+TXT_Codigo.getText().trim()+"'";
            }
            if (st.executeUpdate(cadena) == 1){
                JOptionPane.showMessageDialog(this, "Operacion Satisfactoria");
            }else{
                JOptionPane.showMessageDialog(this, "Error en la Operacion");
            }            
            rs.close();
            st.close();
            cn.close();
        }catch(Exception s){
            System.out.println(s.getMessage());
        }       
        llenarTabla();
        limpiar();
    }//GEN-LAST:event_BTN_ActualizaActionPerformed

    private void TBL_UnidadesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TBL_UnidadesKeyPressed

    }//GEN-LAST:event_TBL_UnidadesKeyPressed

    private void BTN_EliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_EliminarActionPerformed
        String cadena = "delete responsable_uni_organi where responsable_uni_organi.cod_uni_organizativa = '"+TXT_Codigo.getText()+"'";
          try{
            Connection cn;
            Statement st;
            cn = Cls_Conexion.obtenerConexion();
            st = cn.createStatement();
            if (st.executeUpdate(cadena) == 1){
                JOptionPane.showMessageDialog(this, "Operacion Satisfactoria");
            }else{
                JOptionPane.showMessageDialog(this, "Error en la Operacion");
            }            
            st.close();
            cn.close();
            llenarTabla();
        }catch(Exception s){

        }
        limpiar();
    }//GEN-LAST:event_BTN_EliminarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BTN_Actualiza;
    private javax.swing.JButton BTN_Buscar_Jefatura;
    private javax.swing.JButton BTN_Eliminar;
    private javax.swing.JTable TBL_Unidades;
    private javax.swing.JTextField TXT_CodDepen;
    private javax.swing.JTextField TXT_Codigo;
    private javax.swing.JTextField TXT_Jefatura;
    private javax.swing.JTextField TXT_Nivel;
    private javax.swing.JTextField TXT_Nombre_Unidad;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
